library(foreign)

the_prefix <- ""

dta <- read.spss(
    paste0(
        the_prefix, "kffstates/KFF_KentuckySurvey_Data_12.04.15.sav"),
    to.data.frame=T
)

dta$FAVOR <- NA
dta$FAVOR[dta$qn3=="Very favorable"] <- 4
dta$FAVOR[dta$qn3=="Somewhat favorable"] <- 3
dta$FAVOR[dta$qn3=="Somewhat unfavorable"] <- 2
dta$FAVOR[dta$qn3=="Very unfavorable"] <- 1

dta$MEDICAID <- 0
dta$MEDICAID[dta$qnd4a=="Medicaid"] <- 1

dta$MEDICARESR <- 0
dta$MEDICARESR[dta$qnd4a=="Medicare"] <- 1

dta$EMPLINSURE <- 0
dta$EMPLINSURE[dta$qnd4a=="Plan through your or your spouse's employer"] <- 1

dta$COVERED <- 0
dta$COVERED[dta$qnd4=="Covered by health insurance"] <- 1

dta$SELFINSURE <- 0
dta$SELFINSURE[dta$qnd4a=="Plan you purchased yourself"] <- 1

dta$BLACK <- 1*(dta$racesum=="Black Non-Hispanic")
dta$HISPANIC <- 1*(dta$racesum %in% c("White Hispanic","Black Hispanic","Hispanic Unspecified"))

#### note gap between this measure, self-insurance measure
dta$KYNECTPER <- 1*(dta$qn24_1=="You")
dta$KYNECTHOUSE <- 1*(dta$qn24_1=="You" | dta$qn24_2=="Person in your household")
dta$KYNECTTIE <- 1*(dta$qn23=="Yes")

dta$SUBSIDY <- 0
dta$SUBSIDY[dta$qn30=="Yes"] <- 1

### seems to have been asked only of Medicaid
#dta$KYNECT2 <- 0
#dta$KYNECT2[dta$qn29=="From the state marketplace, Kynect"] <- 1

dta$PID5 <- NA
dta$PID5[dta$qnd8=="Republican"] <- 5
dta$PID5[dta$qnd8a=="Republican"] <- 4
dta$PID5[dta$qnd8a %in% c("Independent/don't lean to either party","Other party","Don't Know","Refused")] <- 3
dta$PID5[dta$qnd8a=="Democratic"] <- 2
dta$PID5[dta$qnd8=="Democrat"] <- 1

dta$PID <- NA
dta$PID[dta$PID5==5] <- 3
dta$PID[dta$PID5 %in% c(2,3,4)] <- 2
dta$PID[dta$PID5==1] <- 1

dta$REGISTERED <- 0
dta$REGISTERED[dta$qn32=="Yes"] <- 1

dta$EMPLOYED <- 0
dta$EMPLOYED[dta$qnd3 %in% c("Employed full-time","Employed part-time")] <- 1

dta$RETIRED <- 0
dta$RETIRED[dta$qnd3 %in% c("Retired")] <- 1

dta$INCOME <- NA
dta$INCOME[dta$qnd14=="Less than $20,000"] <- 10
dta$INCOME[dta$qnd14=="$20,000 to less than $30,000"] <- 25
dta$INCOME[dta$qnd14=="$30,000 to less than $40,000"] <- 35
dta$INCOME[dta$qnd14=="$40,000 to less than $50,000"] <- 45
dta$INCOME[dta$qnd14=="$50,000 to less than $75,000"] <- 62.5
dta$INCOME[dta$qnd14=="$75,000 to less than $90,000"] <- 82.5
dta$INCOME[dta$qnd14=="$90,000 to less than $100,000"] <- 95
dta$INCOME[dta$qnd14=="$100,000 or more"] <- 200

dta$EDUC <- NA
dta$EDUC[dta$qnd11=="Less than high school (Grades 1-8 or no formal schooling)"] <- 6
dta$EDUC[dta$qnd11=="High school incomplete (Grades 9-11 or Grade 12 with no diploma)"] <- 10
dta$EDUC[dta$qnd11=="High school graduate (Grade 12 with diploma or GED certificate)"] <- 12
dta$EDUC[dta$qnd11=="Some college, no degree (includes some community college)"] <- 13
dta$EDUC[dta$qnd11=="Two year associate degree from a college or university"] <- 14
dta$EDUC[dta$qnd11=="Four year college or university degree/Bachelor’s degree (e.g., BS, BA, AB)"] <- 16
dta$EDUC[dta$qnd11=="Some postgraduate or professional school, no postgraduate degree"] <- 17
dta$EDUC[dta$qnd11=="Post-graduate or professional degree, including master’s, doctorate, medical, or law degree (e.g., MA, MS, PhD, MD, JD)"] <- 19

dta$VOTED15 <- 0
dta$VOTED15[dta$qn33=="Yes, voted"] <- 1

dta$MALE <- 1*(dta$gender=="Male")
dta$AGE <- as.numeric(as.character(dta$cell0))


lout1a <- lm(
    FAVOR ~ MEDICAID+SELFINSURE+EMPLINSURE+EMPLOYED+MALE+as.factor(INCOME)+AGE+BLACK+HISPANIC+as.factor(PID5)+as.factor(EDUC),
    data=dta[dta$AGE < 65,],
    weights=weight
)

lout1b <- lm(
    FAVOR ~ MEDICAID+SELFINSURE+EMPLINSURE+SUBSIDY+EMPLOYED+COVERED+MALE+as.factor(INCOME)+AGE+BLACK+HISPANIC+as.factor(PID5)+as.factor(EDUC),
    data=dta[dta$AGE < 65,],
    weights=weight
)

lout2a <- lm(
    FAVOR ~ KYNECTPER+MEDICAID+SELFINSURE+EMPLINSURE+EMPLOYED+MALE+as.factor(INCOME)+AGE+BLACK+HISPANIC+as.factor(PID5)+as.factor(EDUC),
    data=dta[dta$AGE < 65,],
    weights=weight
)

lout2b <- lm(
    FAVOR ~ KYNECTPER+MEDICAID+SELFINSURE+EMPLINSURE+SUBSIDY+COVERED+EMPLOYED+MALE+as.factor(INCOME)+AGE+BLACK+HISPANIC+as.factor(PID5)+as.factor(EDUC),
    data=dta[dta$AGE < 65,],
    weights=weight
    )

stargazer(
    lout1a,lout1b,lout2a,lout2b,digits=2,
    ## custom.model.names=c("2014","2015","2016"),
    ## omit=c(
    ##     "MALE","EMPLOYED","INCOME","AGE","BLACK","HISPANIC","PID","EDUC",
    ##     "Constant"
    ## ),
    ## covariate.labels=c(
    ##     "Used Kynect", "Received Medicaid","Purchased Own Insurance","Employer-Provided Insurance",
    ##     "Received Subsidy","Insured"
    ## ),
    omit.stat=c("ser","f","adj.rsq", "rsq"),
    table.layout = "ts", float=F,
    star.cutoffs = c(0.05, 0.01, 0.001),
    out=paste0(
        the_prefix, "figs/",
        "tableA7_kentucky_full_model.tex"
    )
)
